import createDefinition from './mock/createDefinition.json'
/**
 * vite plugin
 */
export default class ConfigureServer {
  static result(data) {
    return {
      data: data || null,
      message: "success",
      successful: true,
      statusCode: "200"
    }
  }

  static query(url, key) {
    const host = "http://localhost:8001"
    let search = new URL(host + url).searchParams
    return search.get(key)
  }

  static write(res, data) {
    let d = ConfigureServer.result(data)
    res.writeHead(200, { "Content-Type": "application/json;charset=UTF-8" })
    res.write(JSON.stringify(d))
    res.end()
  }

  constructor() {
    return {
      name: "vite:configure-server-plugin",
      configureServer: server => {
        this.configServer(server)
      },
      configurePreviewServer: server => {
        this.configServer(server)
      }
    }
  }

  configServer(server) {
    server.middlewares.use("/processDefinition/createDefinition", (req, res, next) => {
      ConfigureServer.write(res, createDefinition)
    })
  }
}
